package action;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import net.sf.jasperreports.engine.JasperCompileManager;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ModelDriven;

import pageModel.Receipt;
import pageModel.SaleStatistic;
import service.ReceiptServiceI;

public class SaleStatisticAction extends BaseAction implements ModelDriven<Receipt>{
	 private List<SaleStatistic> myList;
	 private static final long serialVersionUID = 1L;
     private ReceiptServiceI receiptService;
     Receipt receipt = new Receipt();
	 public Receipt getModel() {
			return receipt;
	}
	 public ReceiptServiceI getReceiptService() {
		    return receiptService;
	}
@Autowired
	public void setReceiptService(ReceiptServiceI receiptService) {
		this.receiptService = receiptService;
	}

		public String execute() throws Exception {
	     myList= receiptService.jasper(receipt);
	       try {
	    	   String reportSource;
	    	   reportSource=ServletActionContext.getServletContext().getRealPath("/jasper/report2.jrxml");
	    	   File parent = new File(reportSource).getParentFile();
	    	   JasperCompileManager.compileReportToFile(reportSource, new File(parent, "report2.jasper").getAbsolutePath());           
	        } catch (Exception e) {
	            e.printStackTrace();
	            return ERROR;
	        } 
	        return SUCCESS;
	    }
	 
	    public List<SaleStatistic> getMyList() {
	        return myList;
	    }
}
